﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foothill.Common;
using Foothill.Service.Validation;
using Foothill.IoC;
using Foothill.Service.UserAccount;
using Foothill.Service.SystemMessage;

namespace Foothill.Web
{
    public partial class RecoverPassword : FoothillBasePage
    {
        private IValidationService _validationService;
        private IUserAccountService _userAccountService;
        private ISystemMessageService _systemMessageService;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                object obj = Request.QueryString[GlobalQueryString.RecoverPwdEmailQsName];

                string email = obj as string;

                if (!string.IsNullOrWhiteSpace(email))
                {
                    txtEmail.Text = email;
                }
            }
        }

        protected void btnRecoverClick(object sender, EventArgs e)
        {
            _validationService = IocContainer.GetInstance<IValidationService>();
            _systemMessageService = IocContainer.GetInstance<ISystemMessageService>();

            string email = txtEmail.Text.Trim();
            if (!_validationService.ValidateEmailAddress(email))
            {
                lblFeedback.Text = _systemMessageService.GetSystemMessage(SystemMessageCategory.Web, SystemMessageCode.InvalidEmailAddress);
                lblFeedback.CssClass = "error-message";
            }
            else
            {
                _userAccountService = IocContainer.GetInstance<IUserAccountService>();
                string feedback;
                if (_userAccountService.RecoverPassword(email, out feedback))
                {
                    lblFeedback.Text = feedback;
                    lblFeedback.CssClass = "ok-message";
                }
                else
                {
                    lblFeedback.Text = feedback;
                    lblFeedback.CssClass = "error-message";
                }
            }
        }
    }
}